/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.boxin.ims.modules.wechat.entity.TimingScore;
import com.boxin.ims.modules.wechat.service.TimingScoreService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;

/**
 * 定点扫描获得积分Controller
 * @author Jakemanse
 * @version 2014-05-05
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/timingScore")
public class TimingScoreController extends BaseController {

	@Autowired
	private TimingScoreService timingScoreService;
	
	@Autowired
	private QRCodeService qrCodeService;
	
	@ModelAttribute
	public TimingScore get(@RequestParam(required=false) Long id) {
		if (id != null){
			return timingScoreService.get(id);
		}else{
			return new TimingScore();
		}
	}
	
	@RequiresPermissions("wechat:timingScore:view")
	@RequestMapping(value = {"list", ""})
	public String list(TimingScore timingScore, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			timingScore.setUser(user);
		}
        Page<TimingScore> page = timingScoreService.find(new Page<TimingScore>(request, response), timingScore); 
        model.addAttribute("page", page);
		return "modules/wechat/timingScoreList";
	}

	@RequiresPermissions("wechat:timingScore:view")
	@RequestMapping(value = "form")
	public String form(TimingScore timingScore, Model model) {
	
		User user = UserUtils.getUser();
		if (timingScore.getUser() == null || timingScore.getUser().getId() <1){
			timingScore.setUser(user);
		}
		
		model.addAttribute("timingScore", timingScore);
		return "modules/wechat/timingScoreForm";
	}

	
	
	@RequiresPermissions("wechat:timingScore:edit")
	@RequestMapping(value = "save")
	public String save(TimingScore timingScore,HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, timingScore)){
			return form(timingScore, model);
		}
		
		//生成二维码
		int width = 300;
		int height = 300;
		//生成二维码
		QRCode qrcode = new QRCode();
		String content = TimingScore.URL_PRIX+timingScore.getUser().getId()+"&tid="+timingScore.getId()+"&med=xxx";
		qrcode.setContent(content);
		String path = WechatUtils.getWechatTimingScoreResourceSavePath(request);
		path += File.separator+"timingscore_"+timingScore.getId();
		FileUtils.createDirectory(path);
		QRCodeUtils.generateQRCodePNG(content, path, width,height);
		qrcode.setFileName(content+".png");
		qrcode.setPngPath(path+".png");
		
		qrcode.setUser(timingScore.getUser());
		qrcode.setType(QRCode.TYPE_TIMING_SCORE);
		qrcode.setName(timingScore.getName());
		
		qrCodeService.save(qrcode);
				
				
		timingScore.setQrcode(qrcode);
		timingScoreService.save(timingScore);
		
		
		//更新二维码
		content = TimingScore.URL_PRIX+timingScore.getUser().getId()+"&tid="+timingScore.getId()+"&med=xxx";
		qrcode.setContent(content);
		path = WechatUtils.getWechatTimingScoreResourceSavePath(request);
		path += File.separator+"timingscore_"+timingScore.getId();
		FileUtils.createDirectory(path);
		QRCodeUtils.generateQRCodePNG(content, path, width,height);
		qrcode.setFileName(content+".png");
		qrcode.setPngPath(path+".png");
		
		timingScore.setQrcode(qrcode);
		timingScoreService.save(timingScore);
		
		
		
		addMessage(redirectAttributes, "保存定点扫描获得积分'" + timingScore.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/timingScore/?repage";
	}
	
	
	
	
	@RequiresPermissions("wechat:timingScore:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		timingScoreService.delete(id);
		addMessage(redirectAttributes, "删除定点扫描获得积分成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/timingScore/?repage";
	}

}
